﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using PJ.Business;
using Microsoft.Practices.Composite.Events;
using PJ.Business.SalePurchase.Sale;
using PJ.Business.Partner;
using PJ.Data;

namespace PJ.Presentation.Partner
{
    public class CustomerListModel : BusinessObjectListViewModel<ICustomerModel>, ICustomerListModel
    {
        public CustomerListModel()
        {
        }
        [Microsoft.Practices.Unity.InjectionConstructor]
        public CustomerListModel(ICustomerListView view, IEventAggregator aggregator, ICustomerService service)
            : base(view, aggregator, service)
        {
            EventAggregator.GetEvent<CustomerSearchResultChangeEvent>().Subscribe(this.SetValues);
        }
        #region Protected

        protected override IBusinessObjectModel CreateNewItem(string globalId)
        {
            CustomerModel model = new CustomerModel(globalId, (ICustomerService)Service);
            model.SetValues(Service.GetData(globalId, GetDataMode.UseCache), false);
            return model;
        }
        protected override IBusinessObjectModel CreateNewItem(IBusinessObject obj)
        {
            return new CustomerModel(obj, (ICustomerService)Service);
        }
        #endregion

        #region Field

        #endregion



        public override void SelectionChange(IBusinessObjectModel businessObject)
        {
            if (businessObject == null)
                EventAggregator.GetEvent<CustomerSelectionEvent>().Publish(null);
            else
                EventAggregator.GetEvent<CustomerSelectionEvent>().Publish(
                    new EntityEventArg() { GlobalId = businessObject.GlobalId, Id = ((IEntityModel)businessObject).Id });
        }

        public override void InitializingNewItem(IBusinessObjectModel newItem)
        {
            throw new NotImplementedException();
        }
    }
}
